package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;

import controller.NewEntityController;

public class NewEntityView extends View<NewEntityController> implements Displayable{

	private JFrame frame;
	
	private static NewEntityView instance = null;
	
	private NewEntityController controller;

	/**
	 * Create the application.
	 */
	private NewEntityView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		setFrame(new JFrame("Editor grupo drink"));
		getFrame().setBounds(200, 100, 450, 300);
		
		JButton btnCargar = new JButton("Entidad Global");
		btnCargar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonEntidadGlobal();
			}
		});
		
		JButton btnCrear = new JButton("Entidad Nueva");
		btnCrear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonEntidadNueva();
			}
		});
		
		JLabel lblBienvenido = new JLabel("Nueva Entidad");
		GroupLayout groupLayout = new GroupLayout(getFrame().getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(70)
							.addComponent(btnCargar, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnCrear, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(188)
							.addComponent(lblBienvenido)))
					.addContainerGap(52, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addComponent(lblBienvenido)
					.addGap(92)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCrear, GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
						.addComponent(btnCargar, GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE))
					.addGap(64))
		);
		getFrame().getContentPane().setLayout(groupLayout);
	}

	public JFrame getFrame() {
		return frame;
	}

	private void setFrame(JFrame frame) {
		this.frame = frame;
	}

	@Override
	public void show() {
		getInstance().getFrame().setVisible(true);
	}
	
	public static NewEntityView getInstance(){
		if (instance == null){
			instance = new NewEntityView();
		}
		return instance;
	}

	@Override
	public void hide() {
		getInstance().getFrame().setVisible(false);
	}

	public NewEntityController getController() {
		return controller;
	}

	public void setController(NewEntityController controller) {
		this.controller = controller;
	}
}
